package cn.com.cifi.mars.util;

import cn.com.cifi.mars.bean.bo.test.TestSignReceiptsBO;
import cn.com.cifi.mars.bean.bo.test.TestSignReceivableBO;
import cn.com.cifi.mars.bean.bo.test.TestSignRecordsBO;
import cn.com.cifi.mars.bean.bo.test.TestSignVersionData;
import cn.com.cifi.mars.bean.dto.MymContractDto;
import cn.com.cifi.mars.entity.SignPlanVersionDetail;
import cn.com.cifi.mars.entity.VsMarsFee;
import cn.com.cifi.mars.entity.VsMarsGetin;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 *
 * 类名称：ResultDetailTemplate 类描述：结果明细表模板 创建人：jyx 创建时间：2020年08月11日
 * 
 * @version 1.0.0
 */

@Slf4j
public class TestSalesDataTemplate {
	private static final String TEMPLATE_PATH = "/template/test_sales_data_template.xlsx";

	private XSSFWorkbook workbook;

	private XSSFSheet sheet;

	// 默认sheet索引
	private int defaultSheetIndex = 0;
	// 数据开始行
	private int dataBeginRow1 = 1;

	// 当前行标
	private int currentDataRowCursor = dataBeginRow1;
	// 单元格样式
	private XSSFCellStyle cellStyle;

	public TestSalesDataTemplate() {
		try {
			InputStream tempInputStream = TestSalesDataTemplate.class.getResourceAsStream(TEMPLATE_PATH);
			if (tempInputStream != null) {
				workbook = new XSSFWorkbook(tempInputStream);
				sheet = workbook.getSheetAt(defaultSheetIndex);
				XSSFRow row = sheet.getRow(dataBeginRow1);
				cellStyle = row.getCell(2).getCellStyle();
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * 批量插入行
	 *
	 * @param list
	 */
	public synchronized void appendRowsOne(List<TestSignRecordsBO> list, int sheetIndex) {
		this.defaultSheetIndex = sheetIndex;
		this.currentDataRowCursor = 1;
		this.sheet = workbook.getSheetAt(defaultSheetIndex);

		for (TestSignRecordsBO bo : list) {

			XSSFRow row = sheet.getRow(currentDataRowCursor);
			if (row == null) {
				row = sheet.createRow(currentDataRowCursor);
			}
			int index = 0;

			this.appendCell(row, index++, bo.getStageName());
			this.appendCell(row, index++, bo.getContractGUID());
			this.appendCell(row, index++, bo.getRoomInfo());
			this.appendCell(row, index++, bo.getYwgsDate().format(DateTimeFormatter.ofPattern(DateUtils.DATE_SMALL_STR)));
			this.appendCell(row, index++, bo.getCjRmbTotal().longValue());
			this.appendCell(row, index++, null == bo.getCloseDate() ? "" : bo.getCloseDate().format(DateTimeFormatter.ofPattern(DateUtils.DATE_SMALL_STR)));
			this.appendCell(row, index++, null == bo.getCloseDate() ? 0 : bo.getCjRmbTotal().longValue());
			this.appendCell(row, index++, null == bo.getAuditDate() ? "" : bo.getAuditDate().format(DateTimeFormatter.ofPattern(DateUtils.DATE_SMALL_STR)));
			this.appendCell(row, index++, null == bo.getAuditDate() ? 0 : bo.getBcTotal().longValue());
			currentDataRowCursor++;
		}
	}

	/**
	 * 批量插入行
	 *
	 * @param list
	 */
	public synchronized void appendRowsTwo(List<TestSignReceivableBO> list, int sheetIndex) {
		this.defaultSheetIndex = sheetIndex;
		this.currentDataRowCursor = 1;
		this.sheet = workbook.getSheetAt(defaultSheetIndex);

		for (TestSignReceivableBO bo : list) {

			XSSFRow row = sheet.getRow(currentDataRowCursor);
			if (row == null) {
				row = sheet.createRow(currentDataRowCursor);
			}
			int index = 0;

			this.appendCell(row, index++, bo.getStageName());
			this.appendCell(row, index++, bo.getContractGUID());
			this.appendCell(row, index++, bo.getRoomInfo());
			this.appendCell(row, index++, bo.getReceiptDate().format(DateTimeFormatter.ofPattern(DateUtils.DATE_SMALL_STR)));
			this.appendCell(row, index++, bo.getReceiptAmount().longValue());
			currentDataRowCursor++;
		}
	}

	/**
	 * 批量插入行
	 *
	 * @param list
	 */
	public synchronized void appendRowsThree(List<TestSignReceiptsBO> list, int sheetIndex) {
		this.defaultSheetIndex = sheetIndex;
		this.currentDataRowCursor = 1;
		this.sheet = workbook.getSheetAt(defaultSheetIndex);

		for (TestSignReceiptsBO bo : list) {

			XSSFRow row = sheet.getRow(currentDataRowCursor);
			if (row == null) {
				row = sheet.createRow(currentDataRowCursor);
			}
			int index = 0;

			this.appendCell(row, index++, bo.getStageName());
			this.appendCell(row, index++, bo.getContractGUID());
			this.appendCell(row, index++, bo.getRoomInfo());
			this.appendCell(row, index++, bo.getGetDate().format(DateTimeFormatter.ofPattern(DateUtils.DATE_SMALL_STR)));
			this.appendCell(row, index++, bo.getRmbAmount().longValue());
			this.appendCell(row, index++, bo.getZlAmount().longValue());
			this.appendCell(row, index++, bo.getClAmount().longValue());
			currentDataRowCursor++;
		}
	}

	public void appendRowsFour(List<TestSignVersionData> dataList,List<String> timeList, int sheetIndex) {
		this.defaultSheetIndex = sheetIndex;
		this.currentDataRowCursor = 0;
		this.sheet = workbook.getSheetAt(defaultSheetIndex);

		XSSFRow row1 = sheet.getRow(currentDataRowCursor);
		if (row1 == null) {
			row1 = sheet.createRow(currentDataRowCursor);
		}
        int index = 0;
        this.appendCell(row1, index++, "项目名称");
        this.appendCell(row1, index++, "分期名称");
        this.appendCell(row1, index++, "版本名称");
        this.appendCell(row1, index++, "组团名称");
        this.appendCell(row1, index++, "组合业态名称");
        for (String tiem : timeList ){
            this.appendCell(row1, index++, tiem);
        }
        currentDataRowCursor++;


		for (TestSignVersionData data : dataList) {
            XSSFRow row = sheet.getRow(currentDataRowCursor);
            if (row == null) {
                row = sheet.createRow(currentDataRowCursor);
            }
            index = 0;
            this.appendCell(row, index++, data.getProjectName());
            this.appendCell(row, index++, data.getStageName());
            this.appendCell(row, index++, data.getVersionName());
            this.appendCell(row, index++, data.getGroupName());
            this.appendCell(row, index++, data.getCombinationProductName());
            for (BigDecimal money : data.getMoneyList() ){
                this.appendCell(row, index++, money.longValue());
            }
            currentDataRowCursor++;
		}
	}

	/**
	 * 插入列
	 *
	 * @param row
	 * @param index
	 * @param value
	 */
	public synchronized void appendCell(XSSFRow row, int index, String value) {
		XSSFCell cell = row.getCell(index);
		if (cell == null) {
			cell = row.createCell(index);
		}
		cell.setCellStyle(cellStyle);
		cell.setCellValue(value == null ? "" : value);
	}

	/**
	 * 插入列
	 *
	 * @param row
	 * @param index
	 * @param value
	 */
	public synchronized void appendCell(XSSFRow row, int index, Long value) {
		XSSFCell cell = row.getCell(index);
		if (cell == null) {
			cell = row.createCell(index);
		}
		cell.setCellStyle(cellStyle);
		cell.setCellValue(value);
	}

	/**
	 * 导出
	 * 
	 * @param outputStream
	 * @throws IOException
	 */
	public void write(OutputStream outputStream) throws IOException {
		workbook.write(outputStream);
	}



}
